package it.thalaz.business.manager;


import it.thalaz.common.exception.GenericException;
import it.thalaz.dataaccess.dao.UtentiDAO;
import it.thalaz.dataaccess.dto.Utenti;
import it.thalaz.dataaccess.dto.UtentiExample;
import it.thalaz.dataaccess.impl.UtentiDAOImpl;
import it.thalaz.dataaccess.sqlMap.ThalazSqlConfig;
import it.thalaz.thalaadm.exception.UtentiException;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * <p>Title: Utenti Manager</p>
 * <p>This class provides Manager interface wrapping DAO object for 'public.' table.utenti</p>
 * <p>Copyright: Copyright (c) 2009</p>
 * <p>Company: D'vel Snc</p>
 *
 * @author $Author: root $
 * @version $Revision: 1.1 $
 */
public class UtentiManager {

	private static final Log LOG = LogFactory.getLog(UtentiManager.class);

    protected static UtentiDAO dao = new UtentiDAOImpl(ThalazSqlConfig.getSqlMapInstance());


	/**
	 * Create a new record in Database.
	 * @param bean   The Object to be inserted.
	 * @return Annunci Object inserted.
	 * @exception    SQLException if something is wrong.
	 */
	public static void create(Utenti bean) throws GenericException {
		try {
			dao.insertUtenti(bean);
		} catch (Exception e) {
			throw new UtentiException(e);
		}
	}



	/**
	 * Update a record in Database.
	 * @param bean   The Object to be saved.
	 * @exception    SQLException if something is wrong.
	 */
	public static void update(Utenti bean) throws GenericException {
		try {
			dao.updateUtentiByPrimaryKeySelective(bean);
		} catch (Exception e) {
			throw new UtentiException(e);
		}
	}

	/**
	 * Delete a record from Database.
	 * @param key    The PK Object to be deleted.
	 * @exception    SQLException if something is wrong.
	 */
	public static void delete(Utenti key) throws GenericException {
		try {
			dao.deleteUtentiByPrimaryKey(key.getId());
		} catch (Exception e) {
			throw new UtentiException(e);
		} 
	}


	/**
	 * Retrive a record from Database.
	 * @param key       The PK Object to be retrived.
	 * @exception       SQLException if something is wrong.
	 * @exception       GenericException if there is no record returned.
	 */
	public static Utenti get(Utenti key) throws GenericException {
		try {
			Utenti bean = dao.selectUtentiByPrimaryKey(key.getId());
			if (bean != null) {
				return bean;
			} else
				throw new UtentiException();
		} catch (Exception e) {
			throw new UtentiException(e);
		} 
	}

	/**
	 * Retrive all records from Database filtered by given object.
	 * @param bean   The value object used to filter results; can be <code>null</code>.
	 * @exception       SQLException if something goes wrong.
	 * @exception       GenericException if there is no record returned.
	 */
	public static Utenti[] getAllUtenti(UtentiExample bean) throws GenericException {
		try {
			bean.setOrderByClause("id desc");
			List<Utenti> beans = dao.selectUtentiByExample(bean);
			if (beans != null) {
				return beans.toArray(new Utenti[]{});
			} else
				throw new UtentiException();
		} catch (Exception e) {
			throw new UtentiException(e);
		}
	}
}
